package com.hsy.swu.hsyutils

import android.content.Context
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.viewbinding.ViewBinding

abstract class HsyFragment<VB : ViewBinding, VM : HsyViewModel> : Fragment() {
    val mBinding by lazy {
        createViewBinding()!!
    }

    val mViewModel by lazy {
        createViewModel()
    }

    private val logName = javaClass.simpleName
    override fun onAttach(context: Context) {
        super.onAttach(context)
        Log.v(TAG, "$logName onAttach")

    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        Log.v(TAG, "$logName onCreate")

    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        Log.v(TAG, "$logName onCreateView")
        return mBinding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        Log.v(TAG, "$logName onViewCreated")

    }

    override fun onStart() {
        super.onStart()
        Log.v(TAG, "$logName onStart")
        mBinding.setFont()

    }

    override fun onResume() {
        super.onResume()
        Log.v(TAG, "$logName onResume")

    }

    override fun onPause() {
        super.onPause()
        Log.v(TAG, "$logName onPause")

    }

    override fun onStop() {
        super.onStop()
        Log.v(TAG, "$logName onStop")

    }

    override fun onDestroyView() {
        super.onDestroyView()
        Log.v(TAG, "$logName onDestroyView")

    }

    override fun onDestroy() {
        super.onDestroy()
        Log.v(TAG, "$logName onDestroy")

    }

    override fun onDetach() {
        super.onDetach()
        Log.v(TAG, "$logName onDetach")

    }

    /**
     * 通过反射创建 viewBinding
     * @return  ViewBinding
     */
    open fun createViewBinding(): VB? {
        return createViewBinding(layoutInflater)
    }

    /**
     * 通过反射创建 viewModel
     * @return  ViewModel
     */
    open fun createViewModel(): VM {
        return createViewModel(this)
    }
}